package cn.sccl.common.util;


import cn.sccl.dirms.security.model.UserPrincipal;
import cn.sccl.dirms.security.model.UserPrincipalDetails;

import javax.servlet.http.HttpSession;

/**
 * Created by 杨浩宇 on 2016-02-01.
 */
public class SecurityUtils {

    public static UserPrincipal getUserPrincipal(HttpSession session) {
        UserPrincipalDetails u = (UserPrincipalDetails)session.getAttribute("user");
        if (u != null) {
            return buildUserPrincipal(u);
        } else {
            return null;
        }
    }

    public static UserPrincipal buildUserPrincipal(Object principal) {
        if (principal instanceof UserPrincipalDetails) {
            UserPrincipalDetails userPrincipalDetails = (UserPrincipalDetails) principal;
            UserPrincipal userPrincpal = new UserPrincipal();
            userPrincpal.setId(userPrincipalDetails.getId());
            userPrincpal.setName(userPrincipalDetails.getName());
            userPrincpal.setUserName(userPrincipalDetails.getUserName());
            userPrincpal.setToken(userPrincipalDetails.getToken());
            return userPrincpal;
        }
        return null;
    }
}
